package com.polaris.live.ui.live.widget.adapter

import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.polaris.live.adapter.live.LiveMessageAdapter
import com.polaris.live.databinding.LiveMessageInviteCallItemBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.MessageUserExtBean
import com.polaris.live.im.sfs.bean.live.payload.InviteCallPayloadBean
import com.polaris.live.ui.message.widget.single_message.AbstractLiveMessageItemHolder
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * 邀请通话消息
 *
 * @author Created by 半仙 on 2023/10/24/024 14:23
 */
class LiveMessageInviteCallItem(
    adapter: LiveMessageAdapter,
    binding: LiveMessageInviteCallItemBinding,
) : AbstractLiveMessageItemHolder<LiveMessageInviteCallItemBinding>(adapter, binding) {

    override fun bind(item: SendLiveMessage) {
        mBinding.apply {
            val ext = item.ext as? MessageUserExtBean?
            val payload = item.payload as? InviteCallPayloadBean?

            if (ext != null && payload != null) {
                tvContent.text = item.content

                userAvatar.setImageUrl(ext.user.avatar, ext.user.userId)
                avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(ext.user.avatarFrame))

                Glide.with(inviteToCall.context)
                    .load(ImageUtils.imgPath(payload.pic))
                    .transform(CircleCrop())
                    .into(inviteToCall)
            } else {
                userAvatar.setImageUrl(null)
                tvContent.text = ""
                avFv.setFrameImage(null)
                inviteToCall.setImageResource(0)
            }
        }
    }
}